package in.ac.iitb.cse.nlp.wordprediction;

import java.io.IOException;
import java.util.List;

import jline.Completor;
import jline.ConsoleReader;

public class WordPredictor {

	private Trainer trainer;
	ConsoleReader consoleReader;

	public WordPredictor() {
		trainer = new Trainer();
		trainer.goDir("", "corpus");
	}

	List<String> predict(String sentence) {
		String[] split = sentence.split(Trainer.WORD_SEPARATOR);
		return trainer.wordMatrix.getSuitablePrediction(split[split.length-1]);
	}

	private void go() {
		initialize();
	}
	
	private void initialize() {
		 try {
			consoleReader = new ConsoleReader();
			consoleReader.addCompletor(new WordCompletor());
			String line = null;
			while((line=consoleReader.readLine("word_predictor>"))!=null){
				if(line.equalsIgnoreCase("exit")||line.equalsIgnoreCase("quit")){
					System.exit(0);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new WordPredictor().go();
	}
	
	public class WordCompletor implements Completor{

		@Override
		public int complete(String buffer, int cursor, List completions) {
			String input=null;
			if(cursor==0){
				input="^";
			}else{
				input = buffer;
			}
			for(String string:predict(input.trim())){
				completions.add(" "+string);
			}
			return cursor;
		}
		
	}
}
